package top.mschen.blog.controller;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;
import top.mschen.blog.controller.feignInterfcace.DemoFegin;
import top.mschen.blog.controller.feignInterfcace.Domain;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
//@CrossOrigin
//设置全局的hystix的配置
@DefaultProperties(defaultFallback = "hiErrorDe")
public class DemoFeign {

    @Autowired
    DemoFegin demoFegin;


    ////////////////////////////hystix 的raribbon 的使用，commandKey 设置指定方法的超时时间 ，，
  @HystrixCommand(fallbackMethod = "hiError",commandProperties = {
//            @HystrixProperty(name="default_executionTimeoutInMilliseconds",value = "3000")
    },commandKey = "sayHi1-server")
    @RequestMapping(value = "/hi")
    public String sayHi1(@RequestParam("name") String name) {
        RestTemplate restTemplate = new RestTemplate();
        Map map = new HashMap();

        return restTemplate.getForObject("http://127.0.0.1:9093/hello?name=1",String.class);
//        return demoFegin.sayHiFromClientOne( name );
    }

    public String hiError(String name) {
        return "hi,"+name+",sorry,error!";
    }
    //全局的默认方法不可以有参数。。。
    public String hiErrorDe() {
        return "hi,"+1+",sorry,error!!!!!!!!!!!!!";
    }

    ////////////////////////////hystix 的raribbon 的使用


    @RequestMapping(value = "/hi1")
    public String sayHiF(@RequestParam("name") String name) {
        return demoFegin.sayHiFromClientOne( name );
    }

    @GetMapping("/domain/get001/{id}")
    public Domain getDomain001(@PathVariable("id") int id){
        return demoFegin.getDomain001(id);
    }

    @GetMapping("/domain/get002/{id}")
    public Domain getDomain002(@PathVariable("id") int id){
        return demoFegin.getDomain002(id);
    }

    //多参数调用 可以是两种
    @GetMapping("/domain/get003")
    public Domain getDomain003(@RequestParam("userName") String userName,@RequestParam("pwd")String pwd,@RequestParam("address")String address){
        return demoFegin.getDomain003(userName,pwd,address);
    }

    @GetMapping("/domain/get004")
    public Domain getDomain004(@RequestParam Map<String,Object> map){
        System.out.println(map);
        return new Domain();
//        return demoFegin.getDomain004(map);
    }

    @PostMapping("/domain/post001")
    public Domain postDomain001(@RequestBody Domain domain){
        return demoFegin.postDomain001(domain);
    }

    @PostMapping("/domain/post002")
    public Domain postDomain002(@RequestBody Domain domain){
        return demoFegin.postDomain002(domain);
    }

    @PostMapping("/post003")
    public List<Domain> postDomain003(@RequestBody Domain domain){
        return demoFegin.postDomain003(domain);
    }

    @Autowired
    private RestTemplate restTemplate;

    @PostMapping("/domain/restTemplate")
    public Domain postDomainRestTemplate(@RequestBody Domain domain){
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(type);
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
        HttpEntity<Domain> httpEntity = new HttpEntity<Domain>(domain, headers);
        ResponseEntity<Domain> domainResponseEntity = restTemplate.postForEntity("http://feignprovider/post003", httpEntity, Domain.class);
        System.out.println(domainResponseEntity);
        return  domainResponseEntity.getBody();
//        return restTemplate.postForObject("http://feignprovider/post003",httpEntity,Domain.class);
//        return restTemplate.getForObject("http://feignprovider/post003",Domain.class);
//        return demoFegin.postDomain001(domain);
    }


    @PostMapping(value = "/uploadFile")
    public String handleFileUpload(@RequestPart("file") MultipartFile file,@RequestParam("title") String title){
        System.out.println(file.getOriginalFilename());
        return demoFegin.handleFileUpload(file,title);
    }

    @PostMapping(value = "/uploadFileMutil")
    public String handleFileUploadMutil(@RequestPart("file")MultipartFile[] files,@RequestParam("title") String title){
        String name="";
        for (MultipartFile file:files){
            name =name+file.getOriginalFilename();
        }
        System.out.println(name);
        return demoFegin.handleFileUploadMutil(files,title);
    }
}
